//
//  LTLTextView.m
//  
//
//  Created by Apple_Lzzy45 on 16/11/4.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "LTLTextView.h"
#import "LTLemoticonButton.h"
#import "LTLEmotion.h"
 

@interface LTLTextView ()

@property(nonatomic , strong) UILabel *placeholderLabel;

@end

@implementation LTLTextView
#pragma mark - 懒加载
///占位提示
-(UILabel *)placeholderLabel
{
    if (!_placeholderLabel) {
        //初始化
        _placeholderLabel = [[UILabel alloc]init];
        //位置
        _placeholderLabel.x = 5;
        _placeholderLabel.y = 8;
        //根据是否有图片而现实占位文字
        _placeholderLabel.hidden = self.hasText;
        
        [_placeholderLabel sizeToFit];
        
        [self addSubview:_placeholderLabel];
    }
    return _placeholderLabel;
}

///相册懒加载
-(LTLXiangCen *)photoView
{
    if (_photoView == nil) {
        _photoView =[[LTLXiangCen alloc]initWithFrame:CGRectMake(0, 99, self.width, self.width)];
//        _photoView.backgroundColor =[UIColor blueColor];
        [self addSubview:_photoView];
        //关闭图片显示,添加图片的时候在显示
        _photoView.hidden = YES;
    }
    return _photoView;
}
//初始化
-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        //注册检测文本改变事件
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];
//        //注册 监听键盘frame改变事件
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UIKeyboardWillChangeFrameNotification object:nil];
        // 开启一个通知接受,输入表情通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LTLemoticonButton:) name:@"LTLemoticonButton" object:nil];
    }
    return self;
}
//输入表情监听
-(void)LTLemoticonButton:(NSNotification *)notification
{
    LTLemoticonButton *userInfo = notification.userInfo[@"LTLemoticonButton"];
    
    LTLEmotion *model = userInfo.emoticon;
    
    BOOL isDelete = userInfo.isDeleteButton;
    
    [self insertEmoticon:model isRemoved:isDelete];
}
- (void)insertEmoticon:(LTLEmotion *)emoticon isRemoved:(BOOL)isRemoved {
    
    if (isRemoved) {
        [self deleteBackward];
        
        return;
    }
    
    if (emoticon.code != nil) {
        
        [self replaceRange:[self selectedTextRange] withText:emoticon.code.emoji];
        
        return;
    }
    

}

/**
 *  监听文字改变
  */
-(void)textChange
{
    //消息循环过程中，定期调用
//    [self setNeedsDisplay];
    
    NSInteger height = ceilf([self sizeThatFits:CGSizeMake(self.bounds.size.width, MAXFLOAT)].height);
    
    if (height > 99 ) {
        _photoView.y = height;
    }
    [self setlabel];
    
}
//每次赋值都要设置一直提示文本
-(void)setPlacehlder:(NSString *)placeholder
{

    _placeholder=placeholder;
//    [self setNeedsDisplay];
    [self setlabel];
    
}
-(void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor =placeholderColor;
//    [self setNeedsDisplay];
    [self setlabel];

}
-(void)setText:(NSString *)text
{
    [super setText:text];
//    [self setNeedsDisplay];
    [self setlabel];
}
-(void)setFont:(UIFont *)font
{
    [super setFont:font];
//    [self setNeedsDisplay];
    [self setlabel];
}
//设置提示文本
-(void)setlabel
{
    //根据有无文本而是否显示文本提示
    self.placeholderLabel.hidden = self.hasText;
    self.placeholderLabel.text = self.placeholder;
    self.placeholderLabel.textColor =self.placeholderColor?self.placeholderColor :[UIColor grayColor];
    self.placeholderLabel.font = self.font;
    [self.placeholderLabel sizeToFit];
}
-(void)dealloc
{
    /// 关闭消息中心
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"销毁");
}
//
//-(void)drawRect:(CGRect)rect
//{
//    if (self.hasText) return;
//
//    NSLog(@"drawRect");
//    
//    CGFloat x =5;
//    CGFloat y =8;
//    CGFloat w =rect.size.width - 2 *x;
//    CGFloat h =rect.size.height -2 * y;
//    CGRect placeholderRect =CGRectMake(x, y,w,h);
//    
//    NSMutableDictionary *attrs=[NSMutableDictionary dictionary];
//    attrs[NSFontAttributeName] =self.font;
//    
////    self.placeholderLabel.text = self.placeholder;
////    self.placeholderLabel.textColor =self.placeholderColor?self.placeholderColor :[UIColor grayColor];
////    self.placeholderLabel.font = self.font;
////    
////    [self.placeholderLabel sizeToFit];
////    self.placeholderLabel.backgroundColor = [UIColor yellowColor];
//    attrs[NSForegroundColorAttributeName]=self.placeholderColor?self.placeholderColor :[UIColor grayColor];
//    
//    
//    [self.placeholder drawInRect:placeholderRect withAttributes:attrs];
//    
//}
@end
